package com.jast.business.controller;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jast.business.domain.BigdataHbaseKv;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jast.common.annotation.Log;
import com.jast.common.enums.BusinessType;
import com.jast.business.domain.BigdataHdfsKv;
import com.jast.business.service.IBigdataHdfsKvService;
import com.jast.common.core.controller.BaseController;
import com.jast.common.core.domain.AjaxResult;
import com.jast.common.utils.poi.ExcelUtil;
import com.jast.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * HDFS监控指标最新值Controller
 *
 * @author Jast
 * @date 2023-02-08
 */
@Controller
@RequestMapping("/business/hdfs")
public class BigdataHdfsKvController extends BaseController
{
    private String prefix = "business/hdfs";

    @Autowired
    private IBigdataHdfsKvService bigdataHdfsKvService;

    @RequiresPermissions("business:hdfs:view")
    @GetMapping()
    public String hdfs()
    {
        return prefix + "/hdfs";
    }

    /**
     * 查询HDFS监控指标最新值列表
     */
    @RequiresPermissions("business:hdfs:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BigdataHdfsKv bigdataHdfsKv)
    {
        startPage();
        List<BigdataHdfsKv> list = bigdataHdfsKvService.selectBigdataHdfsKvList(bigdataHdfsKv);
        return getDataTable(list);
    }

    /**
     * 导出HDFS监控指标最新值列表
     */
    @RequiresPermissions("business:hdfs:export")
    @Log(title = "HDFS监控指标最新值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BigdataHdfsKv bigdataHdfsKv)
    {
        List<BigdataHdfsKv> list = bigdataHdfsKvService.selectBigdataHdfsKvList(bigdataHdfsKv);
        ExcelUtil<BigdataHdfsKv> util = new ExcelUtil<BigdataHdfsKv>(BigdataHdfsKv.class);
        return util.exportExcel(list, "HDFS监控指标最新值数据");
    }

    /**
     * 新增HDFS监控指标最新值
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存HDFS监控指标最新值
     */
    @RequiresPermissions("business:hdfs:add")
    @Log(title = "HDFS监控指标最新值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BigdataHdfsKv bigdataHdfsKv)
    {
        return toAjax(bigdataHdfsKvService.insertBigdataHdfsKv(bigdataHdfsKv));
    }


    /**
     * 更新/新增HDFS监控指标最新值
     */
    @Log(title = "HDFS监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/upsert")
    @ResponseBody
    public AjaxResult upsert(BigdataHdfsKv bigdataHdfsKv)
    {
        return toAjax(bigdataHdfsKvService.upsertBigdataHdfsKv(bigdataHdfsKv));
    }

    /**
     * 修改HDFS监控指标最新值
     */
    @RequiresPermissions("business:hdfs:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BigdataHdfsKv bigdataHdfsKv = bigdataHdfsKvService.selectBigdataHdfsKvById(id);
        mmap.put("bigdataHdfsKv", bigdataHdfsKv);
        return prefix + "/edit";
    }

    /**
     * 修改保存HDFS监控指标最新值
     */
    @RequiresPermissions("business:hdfs:edit")
    @Log(title = "HDFS监控指标最新值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BigdataHdfsKv bigdataHdfsKv)
    {
        return toAjax(bigdataHdfsKvService.updateBigdataHdfsKv(bigdataHdfsKv));
    }

    /**
     * 删除HDFS监控指标最新值
     */
    @RequiresPermissions("business:hdfs:remove")
    @Log(title = "HDFS监控指标最新值", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bigdataHdfsKvService.deleteBigdataHdfsKvByIds(ids));
    }


    @GetMapping("/monitor")
    public String redis()
    {
        return "bigdata/hdfs/hdfs";
    }

    /**
     * 查询Hbase监控指标最新值列表
     */
    @PostMapping("/getBasicInfo")
    @ResponseBody
    public AjaxResult getBasicInfo(BigdataHdfsKv bigdataHbaseKv)
    {
        JSONObject jsonObject = new JSONObject();
        List<BigdataHdfsKv> list = bigdataHdfsKvService.selectBigdataHdfsKvList(bigdataHbaseKv);
        System.out.println("size;"+list.size());
        for (BigdataHdfsKv hbaseKv : list) {
            if(hbaseKv.getHtype().equals("n")){
                switch (hbaseKv.getHkey()){
                    case "CapacityTotalGB":
                        jsonObject.put("aCapacityTotalGB",hbaseKv.getHvalue());
                        break;
                    case "CapacityUsedGB":
                        jsonObject.put("aCapacityUsedGB",hbaseKv.getHvalue());
                        break;
                    case "CapacityRemainingGB":
                        jsonObject.put("aCapacityRemainingGB",hbaseKv.getHvalue());
                        break;
                    case "CapacityUsedNonDFS":
                        jsonObject.put("aCapacityUsedNonDFS",hbaseKv.getHvalue());
                        break;
                    case "TotalLoad":
                        jsonObject.put("aTotalLoad",hbaseKv.getHvalue());
                        break;
                    case "FilesTotal":
                        jsonObject.put("aFilesTotal",hbaseKv.getHvalue());
                        break;
                    case "StartTime":
                        jsonObject.put("nStartTime",hbaseKv.getHvalue());
                        break;
                }
            }
        }

        System.out.println("查询参数:"+ bigdataHbaseKv.getCluster());
        System.out.println(JSON.toJSONString(success(jsonObject)));
        return success(jsonObject);
    }
}
